var Board=require('./grid.js')
function Main(size){
  this.size=size;
  this.startData = 2;//初始填充2个数据
  this.init();
  this.score=0;
  this.isChange=false;
  this.isCombine=false;
  
}
Main.prototype={
  init(){
    this.board = new Board(this.size);
    this.bproto = this.board.__proto__;
    this.setDataRandom();
    this.startData=1;
  },
  setDataRandom(){
    for(var i=0;i<this.startData;i++){
      
      this.addRandomData();
    }
  },
  addRandomData(){
    if (!this.board.cellEmpty()){
      var value=Math.random()<0.9?2:4;
      var cell = this.board.selectCell();
      cell.val = value;
      this.update(cell);
    }
  },
  update(cell){
    this.board.grid[cell.x][cell.y]=cell.val;
  },
  //根据滑动方向，生成数组列表
  formList(dir){
    var list=[[],[],[],[]];
    for(var i=0;i<this.size;i++){
      for (var j = 0; j < this.size; j++) {
        switch (dir){
          case 0:
            list[i].push(this.board.grid[j][i])
            break;
          case 1:
            list[i].push(this.board.grid[i][this.size - 1 - j])
            break;
          case 2:
            list[i].push(this.board.grid[this.size-1-j][i])
            break;
          case 3:
            list[i].push(this.board.grid[i][j])
            break;
        }
      }
    }
    return list;
  },
  changeItem(item){ //将[0202]改为[2200]
    var cnt=0;
    for(var i=0;i<item.length;i++){
      if(item[i]!=''){
        if (cnt!=i){
          //数据有变化
          this.isChange=true;
        }
        item[cnt++]=item[i];
      }
    }
    //[2200]
    for(var j=cnt;j<item.length;j++){
      item[j]='';
    }
    return item;
  },
  combine(list){ //滑动时合并相同的项
    for (var i = 0; i < list.length;i++){
      list[i] = this.changeItem(list[i]);
    }
    for(var i=0;i<this.size;i++){
      for (var j = 1; j < this.size; j++) {
        if (list[i][j - 1] == list[i][j] && list[i][j]!=''){
          list[i][j-1]+=list[i][j];
          //list[i][j-1]=list[i][j-1]+list[i][j];
          list[i][j]='';
          this.score = this.score + list[i][j - 1];
          this.isCombine=true;
        }
      }
    }
    for (var i = 0; i < list.length; i++) {
      list[i] = this.changeItem(list[i]);
    }
    return list;
  },
  move(dir){
    this.board.__proto__=this.bproto;
    var curList=this.formList(dir);
    var list = this.combine(curList);
    var result=[[],[],[],[]];
    for(var i=0;i<this.size;i++){
      for (var j = 0; j < this.size; j++) {
        switch(dir){
          case 0:
            result[i][j]=list[j][i];
            break;
          case 1:
            result[i][j] = list[i][this.size-1-j];
            break;
          case 2:
            result[i][j] = list[j][this.size-1-i];
            break;
          case 3:
            result[i][j] = list[i][j];
            break;
        }
      }
    }
    this.board.grid=result;
    if(this.isCombine){
      this.setDataRandom();
      this.isChange=false;
      this.isCombine=false
    }else{
      if(this.isChange){
        this.setDataRandom();
        this.isChange=false;
      }
    }
    
    return result;
  },
  getScore(){
    return this.score;
  },
  isOver(){
    this.board.__proto__ = this.bproto;
    if(!this.board.cellEmpty()){
        return false;
    }else{
      for(var i=0;i<this.size;i++){ //左右不等
        for(var j=1;j<this.size;j++){
          if (this.board.grid[i][j] == this.board.grid[i][j-1]){
            return false;
          }
        }
      }
      for (var j = 0; j < this.size; j++) { //上下不等
        for (var i= 1; i < this.size; i++) {
          if (this.board.grid[i][j] == this.board.grid[i-1][j]) {
            return false;
          }
        }
      }
    }
    return true;
  }
}
module.exports=Main;